package name.wilbur.course.db.bigone.ui.custom;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

public class ListModel<E> {
	public static final String ADD_ELEMENT = "addElement";
	public static final String REMOVE_ELEMENT = "removeElement";
	private PropertyChangeSupport delegate;
	private List<E> content;

	public ListModel() {
		content = new ArrayList<E>();
		delegate = new PropertyChangeSupport(this);
	}

	public ListModel(int contentSize) {
		content = new ArrayList<E>(contentSize);
		delegate = new PropertyChangeSupport(this);
	}
	
	public ListModel(List<E> list) {
		content = list;
		delegate = new PropertyChangeSupport(this);
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		delegate.addPropertyChangeListener(listener);
	}

	public void firePropertyChange(PropertyChangeEvent evt) {
		delegate.firePropertyChange(evt);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		delegate.removePropertyChangeListener(listener);
	}

	public void add(E e) {
		if (content.add(e))
			firePropertyChange(
					new PropertyChangeEvent(this, ADD_ELEMENT, null, e)
			);
	}

	public void remove(E e) {
		if (content.remove(e))
			firePropertyChange(
					new PropertyChangeEvent(this, REMOVE_ELEMENT, null, e)
			);
	}

	public Object[] elements() {
		return content.toArray();
	}
	
	public void setContent(List<E> list) {
		this.content = list;
	}
	
	public List<E> getContent() {
		return content;
	}
}
